package com.frxuexi.sign.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.entity.SignBean
import com.frxuexi.lib_base.entity.SignBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.sign.R
import com.frxuexi.sign.SignAdapter
import com.frxuexi.sign.SignIntent
import com.frxuexi.sign.SignViewModel
import com.frxuexi.sign.databinding.ActivitySignListBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/sign/SignListActivity")
@AndroidEntryPoint
class SignListActivity : BaseActivity<ActivitySignListBinding,SignIntent,SignViewModel>() {
    override val binding: ActivitySignListBinding by lazy { ActivitySignListBinding.inflate(layoutInflater) }
    override val vm: SignViewModel by viewModels<SignViewModel>()
    val signAdapter by lazy { SignAdapter() }
    val list= mutableListOf<SignBeanItem>()

    var num=0

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        list.addAll(it.data as SignBean)
                        signAdapter.apply {
                            addData(list)
                            notifyDataSetChanged()
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.signRv.apply {
            adapter=signAdapter
            layoutManager=LinearLayoutManager(this@SignListActivity)
        }
        vm.sendIntent(SignIntent.getSigns(SPUtils.getInstance().getInt(Const.UID)))

        binding.signStatusTab.apply {
            addTab(newTab().setText("全部"),true)
            addTab(newTab().setText("迟到"),false)
            addTab(newTab().setText("早退"),false)
            addOnTabSelectedListener(object :OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    tab?.view?.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_solid_blue)
                    signAdapter.apply {
                        data.clear()
                        if(tab?.position==0){
                            addData(list)
                        }else{
                            addData(list.filter { it.status.contains(tab?.text!!) })
                        }
                        notifyDataSetChanged()
                    }


                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.view?.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_solid_lightgray)
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }
            })
        }

        binding.signStatusTab.getTabAt(0)?.view?.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_solid_blue)

        binding.signFilter.setOnClickListener {
//            getSignByDate()
            getSignsByRange()
        }
    }

    fun getSignsByRange(){
        num++
        showDateDialog {date->
            if(num%2==1){
                binding.signDate.setText(date)
            }else{
                var start= binding.signDate.text.toString()
                binding.signDate.append("至${date}")
                val end=date
                signAdapter.apply {
                    data.clear()
                    addData(list.filter { it.morningCheckIn.isNotEmpty() && it.morningCheckIn.split(" ")[0] in start..end })
                    notifyDataSetChanged()
                }
            }
        }
    }


    //根据日期查询考勤
    fun getSignByDate(){
        showDateDialog {date->
            binding.signDate.setText(date)
            signAdapter.apply {
                data.clear()
                addData(list.filter { it.morningCheckIn.contains(date)})
                notifyDataSetChanged()
            }
        }
    }

}